<?php

namespace app\exception;
use Throwable;
use Webman\Exception\BusinessException;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;

class Handler extends ExceptionHandler
{
    /**
     * 不需要要被记录的异常
     * @var string[]
     */
    public $dontReport = [
        BusinessException::class,
    ];

    /**
     * 写入到日志中
     * @param Throwable $exception
     * @return void
     */
    public function report(Throwable $exception): void
    {
        parent::report($exception);
    }


    /**
     * 将相应的一场渲染到HTTP 相应中
     * @param Request $request
     * @param Throwable $exception
     * @return Response
     */
    public function render(Request $request, Throwable $exception): Response
    {
        if ($exception instanceof BusinessException)
        {

            return  new Response($exception->getCode() , [
                'Content-Type' => 'application/json',
            ] , json_encode([
                'code'      => $exception->getCode(),
                'message'   => $exception->getMessage(),
                'file'      =>  $exception->getFile(),
                'line'      =>  $exception->getLine(),
                'exception'         =>  $exception->getTrace(),
            ]));
//            return json(['code'=>$exception->getCode() , 'message' => $exception->getMessage()]);
        }
        return parent::render($request, $exception);
    }
}